package com.imot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imot.pojo.dto.AddReportDataDto;
import com.imot.pojo.entity.ReportFiles;
import com.imot.pojo.vo.ReportDataVo;
import com.imot.response.Result;
import com.imot.service.IReportFilesService;
import com.imot.service.ReportDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ReportDataServiceImpl
 * Description:
 * date: 2023/11/30 11:43
 *
 * @author imot-jiah.wang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReportDataServiceImpl implements ReportDataService {
    private final IReportFilesService reportFilesService;

    @Override
    public Result addReportData(AddReportDataDto addReportDataDto) {
        log.info("~~~~~~~~~~~~~~~~~~{}",addReportDataDto);
        MultipartFile file = addReportDataDto.getFile();
        long sizeInBytes = file.getSize();
        // 将文件大小转换为MB
        double sizeInMB = sizeInBytes / 1024.0 / 1024.0;
        String s = sizeInMB + "MB";
        LocalDateTime now = LocalDateTime.now();
        ReportFiles reportFiles = new ReportFiles();
        BeanUtils.copyProperties(addReportDataDto,reportFiles);
        reportFiles.setUploadTime(now);
        reportFiles.setFileSize(s);
        reportFilesService.save(reportFiles);
        return Result.success();
    }

    @Override
    public Result deleteReportData(Integer id) {
        ReportFiles reportFiles = new ReportFiles();
        reportFiles.setId(id);
        reportFiles.setIsDeleted(1);
        reportFilesService.updateById(reportFiles);
        return Result.success();
    }

    @Override
    public Result updateReportData(ReportFiles reportFiles) {
        reportFilesService.updateById(reportFiles);
        return Result.success();
    }

    @Override
    public Result getReportData(Integer page, Integer limit, LocalDateTime start, LocalDateTime end, String reportType, String reportName) {
        if (page==null||limit==null){
            page = 1;
            limit = 10;
        }
        LambdaQueryWrapper<ReportFiles> wrapper = new LambdaQueryWrapper<>();
        PageHelper.startPage(page,limit);
        if (start!=null&&end!=null){
            wrapper.ge(ReportFiles::getUploadTime,start);
            wrapper.le(ReportFiles::getUploadTime,end);
        }
        if (reportType!=null){
            wrapper.eq(ReportFiles::getReportType,reportType);
        }
        if (reportName!=null){
            wrapper.eq(ReportFiles::getReportName,reportName);
        }
        wrapper.eq(ReportFiles::getIsDeleted,0);
        List<ReportFiles> reportFiles = reportFilesService.list(wrapper);
        PageInfo<ReportFiles> pageInfo = new PageInfo<>(reportFiles);
        Map<String, Object> map = new HashMap<>();
        List<ReportFiles> list = pageInfo.getList();
        List<ReportDataVo> reportDataVos = new ArrayList<>();
        for (ReportFiles files : list) {
            ReportDataVo reportDataVo = new ReportDataVo();
            BeanUtils.copyProperties(files,reportDataVo);
            reportDataVos.add(reportDataVo);
        }

        map.put("total",pageInfo.getTotal());
        map.put("list",reportDataVos);

        return Result.success(map);
    }

    @Override
    public Result getReportDataDetails(Integer id) {
        ReportFiles reportFiles = reportFilesService.getById(id);
        return Result.success(reportFiles);
    }
}

